package com.zhangzhan.gulimallorder.web;

import com.zhangzhan.gulimallorder.service.OrderWebService;
import com.zhangzhan.gulimallorder.vos.ConfirmVo;
import com.zhangzhan.gulimallorder.vos.SubmitOrderRespVo;
import com.zhangzhan.gulimallorder.vos.SubmitPayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * @author 张一恒
 * @version 1.0
 * @description:
 * @date 2024/6/30 8:12
 */
@Controller
public class OrderWebController {

    @Autowired
    private OrderWebService orderWebService;

    @RequestMapping(value = "/{path}.html", method = RequestMethod.GET)
    public String path(@PathVariable("path") String path) {

        return path;
    }


    @RequestMapping(value = "/toTrade", method = RequestMethod.GET)
    public String toTrade(Model model) {
        ConfirmVo vo = orderWebService.toTrade();
        model.addAttribute("confirm", vo);

        return "confirm";
    }

    @RequestMapping(value = "/toPay", method = RequestMethod.POST)
    public String toPay(SubmitPayVo submitPayVo, Model model, RedirectAttributes attributes) {
        SubmitOrderRespVo vo = orderWebService.toPay(submitPayVo);
        switch (vo.getCode()) {
            case 1:
                attributes.addFlashAttribute("toPayMsg", "订单已提交");
                break;
            case 2:
                attributes.addFlashAttribute("toPayMsg", "订单金额错误");
                break;
            case 3:
                attributes.addFlashAttribute("toPayMsg", "商品库存不足");
                break;
            default:
                model.addAttribute("toPay", vo);
                return "pay";
        }
        return "redirect:http://order.gulimall.com/toTrade";
    }
}
